﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using XiAnAirport.DAL;
using XiAnAirport.Models;

namespace XiAnAirport.BUS
{
    public class StatisticsBUS
    {
        public ResponseMessage GetFaultCount(string starttime, string endtime, string sort)
        {
            ResponseMessage message = new ResponseMessage();
            List<Statistics> content = new List<Statistics>();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                content = da.GetFaultCount(starttime, endtime, "");
                content.Sort(delegate (Statistics s1, Statistics s2)
                {
                    if (sort == "asc")
                    {
                        return s1.sum.CompareTo(s2.sum);
                    }
                    else
                    {
                        return s2.sum.CompareTo(s1.sum);
                    }
                });

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = content.Count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询电梯故障排名失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯故障排名";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询电梯故障排名" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetFaultCountList(string ids, string starttime, string endtime, string sort, int Recordhead, int Recordtail)
        {
            ResponseMessage message = new ResponseMessage();
            List<Statistics> content = new List<Statistics>();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                content = da.GetFaultCount(starttime, endtime, ids);
                content.Sort(delegate (Statistics s1, Statistics s2)
                {
                    if (sort == "asc")
                    {
                        return s1.sum.CompareTo(s2.sum);
                    }
                    else
                    {
                        return s2.sum.CompareTo(s1.sum);

                    }
                });
                int count = content.Count;
                if (Recordhead > -1 && Recordtail > 0)
                {
                    if (count < Recordtail)
                    {
                        Recordtail = count;
                    }
                    if (count < Recordhead + Recordtail)
                    {
                        Recordtail = count - Recordhead;
                    }
                    content = content.GetRange(Recordhead, Recordtail);
                }

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询电梯故障排名列表失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯故障排名列表";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询电梯故障排名列表" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetFaultRank(string starttime, string endtime)
        {
            ResponseMessage message = new ResponseMessage();
            List<FaultRank> content = new List<FaultRank>();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                if (!string.IsNullOrEmpty(starttime) && !string.IsNullOrEmpty(endtime))
                {
                    content = da.GetFaultRank(starttime, endtime);
                    content.Sort(delegate (FaultRank s1, FaultRank s2)
                    {
                        return s2.sum.CompareTo(s1.sum);
                    });
                }
                else
                {

                }

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = content.Count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询故障原因排名失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询故障原因排名";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询故障原因排名" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetRepairCount(string starttime, string endtime, string ids)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                List<Statistics> content = da.GetRepairCount(starttime, endtime, ids);
                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = content.Count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询维修次数失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询维修次数";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询维修次数" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetRepairCountList(string starttime, string endtime, string sort, int Recordhead, int Recordtail, string ids)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                List<Statistics> content = da.GetRepairCount(starttime, endtime, ids);
                content.Sort(delegate (Statistics s1, Statistics s2)
                {
                    if (sort == "asc")
                    {
                        return s1.sum.CompareTo(s2.sum);
                    }
                    else
                    {
                        return s2.sum.CompareTo(s1.sum);
                    }
                });
                int count = content.Count;
                if (Recordhead > -1 && Recordtail > 0)
                {
                    if (count < Recordtail)
                    {
                        Recordtail = count;
                    }
                    if (count < Recordhead + Recordtail)
                    {
                        Recordtail = count - Recordhead;
                    }
                    content = content.GetRange(Recordhead, Recordtail);
                }

                message.errcode = 0;
                message.errmsg = "";
                message.data = content;
                message.total = count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询维修次数排名列表失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询维修次数排名列表";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询维修次数排名列表" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetElevatorRuns(int elevatorid, string endtime)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                DateTime end = new DateTime();
                if (!string.IsNullOrEmpty(endtime))
                {
                    end = Convert.ToDateTime(endtime);
                }
                else
                {
                    end = DateTime.Now;
                }
                string start = new DateTime(end.Year, end.Month, 1).AddMonths(-11).ToString("yyyy-MM-dd HH:mm:ss");
                string endtime_1 = new DateTime(end.Year, end.Month, 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
                ElevatorStatistics elevatorStatistics = new ElevatorStatistics();
                elevatorStatistics.sum = da.GetRunsCount(elevatorid, endtime_1, start);
                List<ElevatorRuns> list = new List<ElevatorRuns>();
                for (int i = 0; i < 12; i++)
                {
                    ElevatorRuns elevatorRuns = new ElevatorRuns();
                    DateTime starttime = new DateTime(end.Year, end.Month, 1).AddMonths(i - 11);
                    elevatorRuns.runs = da.GetRunsMonth(elevatorid, starttime.Year, starttime.Month);
                    elevatorRuns.month = starttime.Year.ToString() + "年" + starttime.Month.ToString().PadLeft(2, '0') + "月";
                    list.Add(elevatorRuns);
                }
                elevatorStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = elevatorStatistics;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询运行次数排名失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询运行次数排名";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询运行次数排名" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetElevatorRunsList(string elevatorids, string starttime, string endtime, int Recordhead, int Recordtail)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                ElevatorStatistics elevatorStatistics = new ElevatorStatistics();
                List<ElevatorRuns> list = new List<ElevatorRuns>();
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                elevatorStatistics.sum = da.GetRunsIds(elevatorids, starttime, endtime);
                list = da.GetRunsList(elevatorids, starttime, endtime);
                int count = list.Count;
                if (Recordhead > -1 && Recordtail > 0)
                {
                    if (count < Recordtail)
                    {
                        Recordtail = count;
                    }
                    if (count < Recordhead + Recordtail)
                    {
                        Recordtail = count - Recordhead;
                    }
                    list = list.GetRange(Recordhead, Recordtail);
                }

                elevatorStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = elevatorStatistics;
                message.total = count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询运行次数异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询运行次数排名列表";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询运行次数排名列表" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetElevatorRunTime(int elevatorid, string endtime)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                DateTime end = new DateTime();
                if (!string.IsNullOrEmpty(endtime))
                {
                    end = Convert.ToDateTime(endtime);
                }
                else
                {
                    end = DateTime.Now;
                }
                string start = new DateTime(end.Year, end.Month, 1).AddMonths(-11).ToString("yyyy-MM-dd HH:mm:ss");
                string endtime_1 = new DateTime(end.Year, end.Month, 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
                ElevatorTimeStatistics elevatorTimeStatistics = new ElevatorTimeStatistics();
                elevatorTimeStatistics.sum = da.GetRunTimeCount(elevatorid, endtime_1, start) / 3600;
                List<ElevatorRunTime> list = new List<ElevatorRunTime>();
                for (int i = 0; i < 12; i++)
                {
                    ElevatorRunTime elevatorRunTime = new ElevatorRunTime();
                    DateTime starttime = new DateTime(end.Year, end.Month, 1).AddMonths(i - 11);
                    int timetotal = da.GetRunTimeMonth(elevatorid, starttime.Year, starttime.Month);
                    elevatorRunTime.timesum = timetotal / 3600;
                    elevatorRunTime.month = starttime.Year.ToString() + "年" + starttime.Month.ToString().PadLeft(2, '0') + "月";
                    list.Add(elevatorRunTime);
                }
                elevatorTimeStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = elevatorTimeStatistics;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询运行时间失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询运行时间排名";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询运行时间排名" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetElevatorRunTimeList(string elevatorids, string starttime, string endtime, int Recordhead, int Recordtail)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                ElevatorTimeStatistics elevatorTimeStatistics = new ElevatorTimeStatistics();
                List<ElevatorRunTime> list = new List<ElevatorRunTime>();
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                elevatorTimeStatistics.sum = da.GetElevatorIdsRunTimeCount(elevatorids, starttime, endtime) / 3600;
                list = da.GetRunTimeList(elevatorids, starttime, endtime);
                int count = list.Count;
                if (Recordhead > -1 && Recordtail > 0)
                {
                    if (count < Recordtail)
                    {
                        Recordtail = count;
                    }
                    if (count< Recordhead+ Recordtail)
                    {
                        Recordtail = count-Recordhead;
                    }
                    list = list.GetRange(Recordhead, Recordtail);
                }
                elevatorTimeStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = elevatorTimeStatistics;
                message.total = count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询运行时间排名列表失败";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询运行时间排名列表";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询运行时间排名列表" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetFaultType(string faulttype, string endtime)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                DateTime end = new DateTime();
                if (!string.IsNullOrEmpty(endtime))
                {
                    end = Convert.ToDateTime(endtime);
                }
                else
                {
                    end = DateTime.Now;
                }
                FaultTypeStatistics faultStatistics = new FaultTypeStatistics();
                List<FaultTypeTimes> list = new List<FaultTypeTimes>();
                for (int i = 0; i < 12; i++)
                {
                    FaultTypeTimes faultTimes = new FaultTypeTimes();
                    DateTime starttime = new DateTime(end.Year, end.Month, 1).AddMonths(i - 11);
                    int timetotal = da.GetFaultTypeMonth(faulttype, starttime.Year, starttime.Month);
                    faultTimes.fault = timetotal;
                    faultTimes.month = starttime.Year.ToString() + "年" + starttime.Month.ToString().PadLeft(2, '0') + "月";
                    list.Add(faultTimes);
                }
                faultStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = faultStatistics;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询故障分类异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询故障分类排名";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询故障分类排名" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetFaultTypeList(string faulttypearray, string starttime, string endtime, int Recordhead, int Recordtail)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                FaultTypeStatistics faultStatistics = new FaultTypeStatistics();
                List<FaultTypeTimes> list = new List<FaultTypeTimes>();
                list = da.GetFaultTypeList(faulttypearray, starttime, endtime);
                int count = list.Count;
                if (Recordhead > -1 && Recordtail > 0)
                {
                    if (count < Recordtail)
                    {
                        Recordtail = count;
                    }
                    if (count < Recordhead + Recordtail)
                    {
                        Recordtail = count - Recordhead;
                    }
                    list = list.GetRange(Recordhead, Recordtail);
                }

                faultStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = faultStatistics;
                message.total = count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询故障分类排名列表异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询故障分类排名列表";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询故障分类排名列表" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetElevatorFault(int elevatorid, string endtime)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                ElevatorFaultStatistics elevatorFaultStatistics = new ElevatorFaultStatistics();
                List<ElevatorFaultTimes> list = new List<ElevatorFaultTimes>();
                DateTime end = new DateTime();
                if (!string.IsNullOrEmpty(endtime))
                {
                    end = Convert.ToDateTime(endtime);
                }
                else
                {
                    end = DateTime.Now;
                }
                string start = new DateTime(end.Year, end.Month, 1).AddMonths(-11).ToString("yyyy-MM-dd HH:mm:ss");
                string endtime_1 = new DateTime(end.Year, end.Month, 1).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd HH:mm:ss");
                elevatorFaultStatistics.sum = da.GetFaultSum(elevatorid, endtime_1, start);

                for (int i = 0; i < 12; i++)
                {
                    ElevatorFaultTimes elevatorFaultTimes = new ElevatorFaultTimes();
                    DateTime starttime = new DateTime(end.Year, end.Month, 1).AddMonths(i - 11);
                    int timetotal = da.GetElevatorFaultMonth(elevatorid, starttime.Year, starttime.Month);
                    elevatorFaultTimes.fault = timetotal;
                    elevatorFaultTimes.month = starttime.Year.ToString() + "年" + starttime.Month.ToString().PadLeft(2, '0') + "月";
                    list.Add(elevatorFaultTimes);
                }
                elevatorFaultStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = elevatorFaultStatistics;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询电梯故障次数排名异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯故障次数排名";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询电梯故障次数排名" + e.ToString());
            }
            return message;
        }

        public ResponseMessage GetElevatorFaultList(string elevatorids, string starttime, string endtime, int Recordhead, int Recordtail)
        {
            ResponseMessage message = new ResponseMessage();
            StatisticsDAL da = new StatisticsDAL();
            try
            {
                ElevatorFaultStatistics elevatorFaultStatistics = new ElevatorFaultStatistics();
                List<ElevatorFaultTimes> list = new List<ElevatorFaultTimes>();
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                elevatorFaultStatistics.sum = elevatorAreaDAL.GetFaultListSum(elevatorids, endtime, starttime);
                list = da.GetElevatorFaultList(elevatorids, starttime, endtime);
                int count = list.Count;
                if (Recordhead > -1 && Recordtail > 0)
                {
                    if (count < Recordtail)
                    {
                        Recordtail = count;
                    }
                    if (count < Recordhead + Recordtail)
                    {
                        Recordtail = count - Recordhead;
                    }
                    list = list.GetRange(Recordhead, Recordtail);
                }

                elevatorFaultStatistics.list = list;
                message.errcode = 0;
                message.errmsg = "";
                message.data = elevatorFaultStatistics;
                message.total = count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "查询电梯故障次数排名列表异常";
                message.data = null;
                message.total = 0;
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "查询电梯故障次数排名列表";
                errorLogInfo.errorMsg = e.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("查询电梯故障次数排名列表" + e.ToString());
            }
            return message;
        }

        public ResponseMessage ExportFaultType(string faulttypearray, string starttime, string endtime, string path)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                StatisticsDAL da = new StatisticsDAL();
                FaultTypeStatistics faultStatistics = new FaultTypeStatistics();
                List<FaultTypeTimes> list = new List<FaultTypeTimes>();
                list = da.GetFaultTypeList(faulttypearray, starttime, endtime);
                if (list.Count > 0)
                {
                    if (list.Count < 65536)
                    {
                        string kssj = string.Empty;
                        string jssj = string.Empty;
                        if (!string.IsNullOrEmpty(starttime))
                        {
                            kssj = Convert.ToDateTime(starttime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            kssj = Convert.ToDateTime(da.QueryFaultTypeListTime(faulttypearray)).ToString("yyyy-MM-dd");
                        }
                        if (!string.IsNullOrEmpty(endtime))
                        {
                            jssj = Convert.ToDateTime(endtime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            jssj = DateTime.Now.ToString("yyyy-MM-dd");
                        }
                        string sFileName = "故障分类统计.xlsx";

                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))

                        {
                            // 添加worksheet
                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障分类统计");
                            //添加头
                            worksheet.Cells[1, 1].Value = "故障类型";
                            worksheet.Cells[1, 2].Value = "故障次数";
                            worksheet.Cells[1, 3].Value = "开始时间";
                            worksheet.Cells[1, 4].Value = "结束时间";
                            //添加值
                            for (int i = 0; i < list.Count; i++)
                            {
                                FaultTypeTimes faultTypeTimes = list[i];
                                worksheet.Cells[i + 2, 1].Value = faultTypeTimes.faultname;
                                worksheet.Cells[i + 2, 2].Value = faultTypeTimes.fault;
                                worksheet.Cells[i + 2, 3].Value = kssj;
                                worksheet.Cells[i + 2, 4].Value = jssj;
                            }
                            package.Save();
                        }
                        response.errcode = 0;
                        response.errmsg = "";
                        response.data = "/" + sFileName;
                    }
                    else
                    {
                        response.errcode = 1;
                        response.errmsg = "数据量超过65535条，无法导出";
                    }
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "无数据导出";
                }
            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出故障分类统计";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出故障分类统计" + ex.ToString());
            }
            return response;
        }

        public ResponseMessage ExportElevatorFaultSum(string elevatorids, string starttime, string endtime, string path)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                StatisticsDAL da = new StatisticsDAL();
                ElevatorFaultStatistics elevatorFaultStatistics = new ElevatorFaultStatistics();
                List<ElevatorFaultTimes> list = new List<ElevatorFaultTimes>();
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                list = da.GetElevatorFaultList(elevatorids, starttime, endtime);
                if (list.Count > 0)
                {
                    if (list.Count < 65536)
                    {
                        string kssj = string.Empty;
                        string jssj = string.Empty;
                        if (!string.IsNullOrEmpty(starttime))
                        {
                            kssj = Convert.ToDateTime(starttime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            kssj = Convert.ToDateTime(da.QueryElevatorFaultListTime(elevatorids)).ToString("yyyy-MM-dd");
                        }
                        if (!string.IsNullOrEmpty(endtime))
                        {
                            jssj = Convert.ToDateTime(endtime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            jssj = DateTime.Now.ToString("yyyy-MM-dd");
                        }
                        string sFileName = "故障次数统计.xlsx";

                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))

                        {
                            // 添加worksheet
                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障次数统计");

                            //添加头
                            worksheet.Cells[1, 1].Value = "设备名称";
                            worksheet.Cells[1, 2].Value = "设备编号";
                            worksheet.Cells[1, 3].Value = "故障次数";
                            worksheet.Cells[1, 4].Value = "开始时间";
                            worksheet.Cells[1, 5].Value = "结束时间";

                            //添加值
                            for (int i = 0; i < list.Count; i++)
                            {
                                ElevatorFaultTimes faultTypeTimes = list[i];
                                worksheet.Cells[i + 2, 1].Value = faultTypeTimes.elevatorName;
                                worksheet.Cells[i + 2, 2].Value = faultTypeTimes.elevatorNo;
                                worksheet.Cells[i + 2, 3].Value = faultTypeTimes.fault;
                                worksheet.Cells[i + 2, 4].Value = kssj;
                                worksheet.Cells[i + 2, 5].Value = jssj;
                            }
                            package.Save();
                        }
                        response.errcode = 0;
                        response.errmsg = "";
                        response.data = "/" + sFileName;
                    }
                    else
                    {
                        response.errcode = 1;
                        response.errmsg = "数据量超过65535条，无法导出";
                    }
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "无导出数据";
                }

            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出故障次数统计";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出故障次数统计" + ex.ToString());
            }
            return response;
        }

        public ResponseMessage ExportElevatorRuns(string elevatorids, string starttime, string endtime, string path)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                StatisticsDAL da = new StatisticsDAL();
                ElevatorStatistics elevatorStatistics = new ElevatorStatistics();
                List<ElevatorRuns> list = new List<ElevatorRuns>();
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                list = da.GetRunsList(elevatorids, starttime, endtime);

                if (list.Count > 0)
                {
                    if (list.Count < 65536)
                    {
                        string kssj = string.Empty;
                        string jssj = string.Empty;
                        if (!string.IsNullOrEmpty(starttime))
                        {
                            kssj = Convert.ToDateTime(starttime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            kssj = Convert.ToDateTime(da.QueryRunsListTime(elevatorids)).ToString("yyyy-MM-dd");
                        }
                        if (!string.IsNullOrEmpty(endtime))
                        {
                            jssj = Convert.ToDateTime(endtime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            jssj = DateTime.Now.ToString("yyyy-MM-dd");
                        }
                        string sFileName = "运行次数统计.xlsx";
                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))
                        {
                            // 添加worksheet

                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("运行次数统计");

                            //添加头
                            worksheet.Cells[1, 1].Value = "设备名称";
                            worksheet.Cells[1, 2].Value = "设备编号";
                            worksheet.Cells[1, 3].Value = "开始时间";
                            worksheet.Cells[1, 4].Value = "结束时间";
                            worksheet.Cells[1, 5].Value = "运行时间";

                            //添加值
                            for (int i = 0; i < list.Count; i++)
                            {
                                ElevatorRuns elevatorRuns = list[i];
                                worksheet.Cells[i + 2, 1].Value = elevatorRuns.elevatorName;
                                worksheet.Cells[i + 2, 2].Value = elevatorRuns.elevatorNo;
                                worksheet.Cells[i + 2, 3].Value = kssj;
                                worksheet.Cells[i + 2, 4].Value = jssj;
                                worksheet.Cells[i + 2, 5].Value = elevatorRuns.runs;
                            }
                            package.Save();
                        }
                        response.errcode = 0;
                        response.errmsg = "";
                        response.data = "/" + sFileName;
                    }
                    else
                    {
                        response.errcode = 1;
                        response.errmsg = "数据量超过65535条，无法导出";
                    }
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "无导出数据";
                }
            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出运行次数统计";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出运行次数统计" + ex.ToString());
            }
            return response;
        }

        public ResponseMessage ExportRepairCount(string starttime, string endtime, string sort, string path, string ids)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                StatisticsDAL da = new StatisticsDAL();
                List<Statistics> list = da.GetRepairCount(starttime, endtime, ids);
                list.Sort(delegate (Statistics s1, Statistics s2)
                {
                    if (sort == "asc")
                    {
                        return s1.sum.CompareTo(s2.sum);
                    }
                    else
                    {
                        return s2.sum.CompareTo(s1.sum);
                    }
                });
                if (list.Count < 65536)
                {
                    string kssj = string.Empty;
                    string jssj = string.Empty;
                    if (!string.IsNullOrEmpty(starttime))
                    {
                        kssj = Convert.ToDateTime(starttime).ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        kssj = Convert.ToDateTime(da.GetRepairCountTime(ids)).ToString("yyyy-MM-dd");
                    }
                    if (!string.IsNullOrEmpty(endtime))
                    {
                        jssj = Convert.ToDateTime(endtime).ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        jssj = DateTime.Now.ToString("yyyy-MM-dd");
                    }
                    string sFileName = "维修次数统计图.xlsx";

                    FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                    file.Delete();
                    ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                    using (ExcelPackage package = new ExcelPackage(file))

                    {
                        // 添加worksheet

                        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("维修次数统计图");

                        //添加头
                        worksheet.Cells[1, 1].Value = "排名";
                        worksheet.Cells[1, 2].Value = "设备名称";
                        worksheet.Cells[1, 3].Value = "设备编号";
                        worksheet.Cells[1, 4].Value = "维修次数";
                        worksheet.Cells[1, 5].Value = "开始时间";
                        worksheet.Cells[1, 6].Value = "结束时间";
                        //添加值

                        for (int i = 0; i < list.Count; i++)
                        {
                            Statistics statistics = list[i];
                            worksheet.Cells[i + 2, 1].Value = i + 1;
                            worksheet.Cells[i + 2, 2].Value = statistics.elevatorName;
                            worksheet.Cells[i + 2, 3].Value = statistics.elevatorNo;
                            worksheet.Cells[i + 2, 4].Value = statistics.sum;
                            worksheet.Cells[i + 2, 5].Value = kssj;
                            worksheet.Cells[i + 2, 6].Value = jssj;
                        }
                        package.Save();

                    }
                    response.errcode = 0;
                    response.errmsg = "";
                    response.data = "/" + sFileName;
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "数据量超过65535条，无法导出";
                }

            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出维修次数统计";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出维修次数统计" + ex.ToString());
            }
            return response;
        }

        public ResponseMessage ExportElevatorRunsTime(string elevatorids, string starttime, string endtime, string path)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                StatisticsDAL da = new StatisticsDAL();
                ElevatorTimeStatistics elevatorTimeStatistics = new ElevatorTimeStatistics();
                List<ElevatorRunTime> list = new List<ElevatorRunTime>();
                ElevatorAreaDAL elevatorAreaDAL = new ElevatorAreaDAL();
                list = da.GetRunTimeList(elevatorids, starttime, endtime);
                if (list.Count > 0)
                {
                    if (list.Count < 65536)
                    {
                        string kssj = string.Empty;
                        string jssj = string.Empty;
                        if (!string.IsNullOrEmpty(starttime))
                        {
                            kssj = Convert.ToDateTime(starttime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            kssj = Convert.ToDateTime(da.QueryRunTimeListTime(elevatorids)).ToString("yyyy-MM-dd");
                        }
                        if (!string.IsNullOrEmpty(endtime))
                        {
                            jssj = Convert.ToDateTime(endtime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            jssj = DateTime.Now.ToString("yyyy-MM-dd");
                        }

                        string sFileName = "运行时间统计.xlsx";

                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))

                        {
                            // 添加worksheet
                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("运行时间统计");

                            //添加头
                            worksheet.Cells[1, 1].Value = "设备名称";
                            worksheet.Cells[1, 2].Value = "设备编号";
                            worksheet.Cells[1, 3].Value = "开始时间";
                            worksheet.Cells[1, 4].Value = "结束时间";
                            worksheet.Cells[1, 5].Value = "运行时间";

                            //添加值
                            for (int i = 0; i < list.Count; i++)
                            {
                                ElevatorRunTime elevatorRunTime = list[i];
                                worksheet.Cells[i + 2, 1].Value = elevatorRunTime.elevatorName;
                                worksheet.Cells[i + 2, 2].Value = elevatorRunTime.elevatorNo;
                                worksheet.Cells[i + 2, 3].Value = kssj;
                                worksheet.Cells[i + 2, 4].Value = jssj;
                                worksheet.Cells[i + 2, 5].Value = elevatorRunTime.timesum;
                            }
                            package.Save();
                        }
                        response.errcode = 0;
                        response.errmsg = "";
                        response.data = "/" + sFileName;
                    }
                    else
                    {
                        response.errcode = 1;
                        response.errmsg = "数据量超过65535条，无法导出";
                    }
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "无数据导出";
                }
            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出运行时间统计";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出运行时间统计" + ex.ToString());
            }
            return response;
        }

        public ResponseMessage ExportElevatorFaultRank(string starttime, string endtime, string sort, string path, string ids)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                StatisticsDAL da = new StatisticsDAL();
                List<Statistics> content = new List<Statistics>();
                content = da.GetFaultCount(starttime, endtime, ids);
                content.Sort(delegate (Statistics s1, Statistics s2)
                {
                    if (sort == "asc")
                    {
                        return s1.sum.CompareTo(s2.sum);
                    }
                    else
                    {
                        return s2.sum.CompareTo(s1.sum);
                    }
                });
                if (content.Count > 0)
                {
                    if (content.Count < 65536)
                    {
                        string kssj = string.Empty;
                        string jssj = string.Empty;
                        if (!string.IsNullOrEmpty(starttime))
                        {
                            kssj = Convert.ToDateTime(starttime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            kssj = Convert.ToDateTime(da.QueryFaultCountTime(ids)).ToString("yyyy-MM-dd");
                        }
                        if (!string.IsNullOrEmpty(endtime))
                        {
                            jssj = Convert.ToDateTime(endtime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            jssj = DateTime.Now.ToString("yyyy-MM-dd");
                        }
                        string sFileName = "故障排名统计.xlsx";

                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))

                        {
                            // 添加worksheet
                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障排名统计");

                            //添加头
                            worksheet.Cells[1, 1].Value = "排名";
                            worksheet.Cells[1, 2].Value = "设备名称";
                            worksheet.Cells[1, 3].Value = "设备编号";
                            worksheet.Cells[1, 4].Value = "故障次数";
                            worksheet.Cells[1, 5].Value = "开始时间";
                            worksheet.Cells[1, 6].Value = "结束时间";
                            //添加值
                            for (int i = 0; i < content.Count; i++)
                            {
                                Statistics statistics = content[i];
                                worksheet.Cells[i + 2, 1].Value = i + 1;
                                worksheet.Cells[i + 2, 2].Value = statistics.elevatorName;
                                worksheet.Cells[i + 2, 3].Value = statistics.elevatorNo;
                                worksheet.Cells[i + 2, 4].Value = statistics.sum;
                                worksheet.Cells[i + 2, 5].Value = kssj;
                                worksheet.Cells[i + 2, 6].Value = jssj;
                            }
                            package.Save();
                        }
                        response.errcode = 0;
                        response.errmsg = "";
                        response.data = "/" + sFileName;
                    }
                    else
                    {
                        response.errcode = 1;
                        response.errmsg = "数据量超过65535条，无法导出";
                    }
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "无数据,导出失败";
                }
            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出故障排名统计";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出故障排名统计" + ex.ToString());
            }
            return response;
        }

        public ResponseMessage ExportFaultListByElevator(string starttime, string endtime, string sort, string path, string ids)
        {
            ResponseMessage response = new ResponseMessage();
            try
            {
                StatisticsDAL da = new StatisticsDAL();
                List<Statistics> content = new List<Statistics>();
                content = da.GetFaultCount(starttime, endtime, ids);

                if (content.Count > 0)
                {
                    if (content.Count < 65536)
                    {
                        string kssj = string.Empty;
                        string jssj = string.Empty;
                        if (!string.IsNullOrEmpty(starttime))
                        {
                            kssj = Convert.ToDateTime(starttime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            kssj = Convert.ToDateTime(da.QueryFaultCountTime(ids)).ToString("yyyy-MM-dd");
                        }
                        if (!string.IsNullOrEmpty(endtime))
                        {
                            jssj = Convert.ToDateTime(endtime).ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            jssj = DateTime.Now.ToString("yyyy-MM-dd");
                        }
                        string sFileName = "故障记录统计.xlsx";

                        FileInfo file = new FileInfo(Path.Combine(path, sFileName));

                        file.Delete();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage package = new ExcelPackage(file))

                        {
                            // 添加worksheet
                            ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("故障记录统计");

                            //添加头
                            worksheet.Cells[1, 1].Value = "设备名称";
                            worksheet.Cells[1, 2].Value = "设备编号";
                            worksheet.Cells[1, 3].Value = "故障描述";
                            worksheet.Cells[1, 4].Value = "故障时间";
                            worksheet.Cells[1, 5].Value = "结束时间";
                            //添加值
                            for (int i = 0; i < content.Count; i++)
                            {
                                Statistics statistics = content[i];
                                worksheet.Cells[i + 2, 2].Value = statistics.elevatorName;
                                worksheet.Cells[i + 2, 3].Value = statistics.elevatorNo;
                                worksheet.Cells[i + 2, 4].Value = statistics.sum;
                                worksheet.Cells[i + 2, 5].Value = kssj;
                                worksheet.Cells[i + 2, 6].Value = jssj;
                            }
                            package.Save();
                        }
                        response.errcode = 0;
                        response.errmsg = "";
                        response.data = "/" + sFileName;
                    }
                    else
                    {
                        response.errcode = 1;
                        response.errmsg = "数据量超过65535条，无法导出";
                    }
                }
                else
                {
                    response.errcode = 1;
                    response.errmsg = "无数据,导出失败";
                }
            }
            catch (Exception ex)
            {
                response.errcode = 1;
                response.errmsg = "导出失败";
                LogBUS logBUS = new LogBUS();
                ErrorLogInfo errorLogInfo = new ErrorLogInfo();
                errorLogInfo.operation = "导出故障记录统计";
                errorLogInfo.errorMsg = ex.ToString();
                logBUS.InsertErrorLog(errorLogInfo);
                LogHelper.Log("导出故障记录统计" + ex.ToString());
            }
            return response;
        }

    }
}
